package cn.billy.pet.controller;

import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.utils.LoginContext;
import cn.billy.org.domain.Employee;
import cn.billy.org.service.IEmployeeService;
import cn.billy.pet.service.IPetService;
import cn.billy.pet.domain.Pet;
import cn.billy.pet.query.PetQuery;
import cn.billy.basic.utils.AjaxResult;
import cn.billy.basic.utils.PageList;
import cn.billy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    @Autowired
    private IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest req){
        Logininfo logininfo = LoginContext.getLogininfo(req);
        // 1.根据LogininfoId查询employee的shopId，再设置到pet的shopId
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        pet.setShopId(employee.getShopId());
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.save(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet getById(@PathVariable("id")Long id)
    {
        System.out.println("1111111111:"+id);
        return petService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> loadAll(){

        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> queryData(@RequestBody PetQuery query)
    {

        return petService.queryData(query);
    }

    @PostMapping("/onsale")
    public AjaxResult onSalePet(@RequestBody List<Long> ids){
        try {
            petService.onSalePet(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSalePet(@RequestBody List<Long> ids){
        try {
            petService.offSalePet(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}
